package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.UserDaoImpl;
import com.ysd.entity.User;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminUserServlet extends BaseServlet {
	/**
	 * 获取留言信息
	 * localhost:8080/shangcheng/adminUser?reqName=getMessageByStatus
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getMessageByStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		UserDaoImpl uDao=new UserDaoImpl();
		String status = request.getParameter("status");
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<User> list=uDao.getUserByStatusMyPage(status, page, size);//分页查到的产品分类集合
		
		
		Gson gson=new Gson();
		String res = gson.toJson(list);
		out.print(res);
		
		out.flush();
		out.close();
	}
	//localhost:8080/shangcheng/adminUser?reqName=getAllMessageByStatus
	public void getAllMessageByStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		UserDaoImpl uDao=new UserDaoImpl();
		String status = request.getParameter("status");
		List<User> list=uDao.getUserByStatus(status);
		
		
		Gson gson=new Gson();
		String res = gson.toJson(list.size());
		out.print(res);
		
		out.flush();
		out.close();
	}
	
	/**
	 * 获取留言信息
	 * localhost:8080/shangcheng/adminUser?reqName=getMessageById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getMessageById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		UserDaoImpl uDao=new UserDaoImpl();
		int id = Integer.parseInt(request.getParameter("id"));
		User user=uDao.getUserById(id);
		
		
		Gson gson=new Gson();
		String res = gson.toJson(user);
		out.print(res);
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/shangcheng/adminUser?reqName=updateMessageById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateMessageById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		UserDaoImpl uDao=new UserDaoImpl();
		int id = Integer.parseInt(request.getParameter("id"));
		
		User user = uDao. getUserById(id); //根据id查询留言信息
		if (user.getStatus().equals("已读") || user.getStatus().equals("已回复")) {
		out . print(Result. toClient(true, "不需要修改",""));
		}else{//否则需要修改
			user=new User(id, "已读", null);
			int n = uDao.updateMessageById(user);
			
			if(n>0){
				out.print(Result.toClient(true,"修改成功",""));
			} else {
				out.print(Result.toClient(false,"修改失败",""));
			}
		}
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/shangcheng/adminUser?reqName=updateMessageReplyById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateMessageReplyById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		UserDaoImpl uDao=new UserDaoImpl();
		int id = Integer.parseInt(request.getParameter("id"));
		String reply = request.getParameter("reply");

		User user=new User(id, null, reply);
		int n = uDao.updateMessageReplyById(user);
		
		if(n>0){
			out.print(Result.toClient(true,"回复成功",""));
			} else {
			out.print(Result.toClient(false,"回复失败",""));
			}

		
		out.flush();
		out.close();
	}
	/**
	 * 批量删除
	 * localhost:8080/shangcheng/adminUser?reqName=deleteMessageReplyByIds
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void deleteMessageReplyByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		UserDaoImpl uDao=new UserDaoImpl();
		String ids = request.getParameter("ids");
		int n = uDao.deleteMessageByIds(ids);
		
		if(n>0){
			out.print(Result.toClient(true,"删除成功",""));
			} else {
			out.print(Result.toClient(false,"删除失败",""));
			}

		
		out.flush();
		out.close();
	}
	
	/**
	 * 删除留言
	 * localhost:8080/shangcheng/adminUser?reqName=delLiuYan
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */

	public void delLiuYan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("userid"));
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		UserDaoImpl uDao = new UserDaoImpl();
		int n = uDao.deluserId(id);
		
		if(n>0){
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		out.flush();
		out.close();
	}

}
